package com.pan.behavioral.command.order.impl;

import com.pan.behavioral.command.order.MyCommand;
import com.pan.behavioral.command.order.MyScreenCommand;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class SaveScreen extends MyScreenCommand {


    private Robot robot;{
        try {
            robot = new Robot();
        } catch (AWTException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void execute() {
        Rectangle screenRect = new Rectangle(super.x,super.y,super.width,super.height);
        BufferedImage screen = robot.createScreenCapture(screenRect);
        try {
            ImageIO.write(screen,"png",new File("src/main/resources/command/screen.jpg"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        JFrame tipsFrame = new JFrame("tips");
        tipsFrame.setDefaultCloseOperation(2);
        tipsFrame.add(new JLabel("截屏成功！保存结果在：src/main/resources/command/screen.jpg"));
        tipsFrame.pack();
        tipsFrame.setVisible(true);
    }


}
